# -*- coding: UTF-8 -*-

from datetime import datetime
import sys
from sqlalchemy import and_,  func, or_
from typing import Dict, List, Any, Generator

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct
from sqlalchemy.orm import Session, relationship
from common.database import Base
from common.database import SessionMixin
from common.utils import is_empty
from core.logger import logger



__all__ = ['NCLogAlarmClassify']

class NCLogAlarmClassify(Base, SessionMixin):
    '''
    日志信息归类表
    '''
    __tablename__ = "nc_log_alarm_classify"
    name = Column(String(32), nullable=False, index=True, comment='归类名称')
    keyword = Column(String(128), nullable=False, comment='关键字信息')
    update_at = Column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    comment = Column(String(256), nullable=True, comment='记录说明')


    __table_args__ = (
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic',
        }
    )

    def __repr__(self):
        return f"<nc_log_alarm_classify='{self.name}'>"


    def object_to_dict(self, db: Session):
        result = {
            key: getattr(self, key)
            for key in self.__mapper__.c.keys()
        }

        return result



    @classmethod
    def get_keywords_for_classify(cls, db: Session):
        """
        获取归类相关的关键字信息
        """
        object_info = db.query(cls).with_entities(
            cls.name,
            func.group_concat(cls.keyword, SEPARATOR="|").label('keywords')
        ).group_by(cls.name).to_dict()
 
        return object_info